/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.discoveryservice.business.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class DiscoveryServiceDAO {

	//private final Map<ServiceID, DiscoveryService> dsNetwork;
	private final List<String> dsNetwork;
	private final Set<String> peersAddresses;
	
	
	/**
	 * @param application
	 */
	public DiscoveryServiceDAO() {
		//this.dsNetwork = new HashMap<ServiceID, DiscoveryService>();
		this.dsNetwork = new ArrayList<String>();
		this.peersAddresses = new HashSet<String>();
	}

	public void peerIsDown(String peerUserAtServer) {
		peersAddresses.remove(peerUserAtServer);
	}

	public boolean isLogged(String peerUserAtServer) {
		return peersAddresses.contains(peerUserAtServer);
	}

	/*public boolean putDiscoveryService(ServiceID serviceId, DiscoveryService discoveryService) {
		return dsNetwork.put(serviceId, discoveryService) == null;
	}*/
	
	public boolean addDiscoveryService(String dsAddress) {
		return this.dsNetwork.add(dsAddress);
	}

	/*public List<DiscoveryService> getDiscoveryServices() {
		return new LinkedList<DiscoveryService>(dsNetwork.values());
	}*/

	/*public List<String> getDiscoveryServicesAddresses() {
		List<String> addresses = new LinkedList<String>();
		for (ServiceID sID : dsNetwork.keySet()) {
			addresses.add(sID.getContainerID().getUserAtServer());
		}
		return addresses;
	}*/
	
	public List<String> getDiscoveryServicesAddresses() {
		return this.dsNetwork;
	}

	/*public List<ServiceID> getDiscoveryServicesIDs() {
		return new LinkedList<ServiceID>(dsNetwork.keySet());
	}*/
	
	/*public boolean removeFromNetwork(ServiceID serviceID) {
		return dsNetwork.remove(serviceID) != null;
	}*/
	public boolean removeFromNetwork(String dsAddress) {
		return dsNetwork.remove(dsAddress);
	}

	public boolean peerIsUp( String userAtServer ) {
		return this.peersAddresses.add(userAtServer);
	}

	public List<String> getOnlinePeers() {
		return new LinkedList<String>(peersAddresses);
	}

	/*public Map<ServiceID, DiscoveryService> getDiscoveryServicesMap() {
		return new HashMap<ServiceID,DiscoveryService>(dsNetwork);
	}*/
}
